﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace sharpCmd.Classes.IO
{
    /// <summary>
    /// Manages list of drives and their labels
    /// </summary>
    //TODO DrivesManager - read labels asynchronously!
    class DrivesManager
    {
        private static DrivesManager instance;
        private static readonly object lockObject = new object();
        private List<DriveInformation> drives;

        public static DrivesManager Instance
        {
            get
            {
                lock (lockObject)
                {
                    if (instance == null)
                        instance = new DrivesManager();
                    return instance;
                }
            }
        }

        public DrivesManager()
        {
            RefreshDrives();
        }

        public List<DriveInformation> GetDrives()
        {
            return drives;
        }

        public void RefreshDrives()
        {
            drives = new List<DriveInformation>();
            DriveInfo[] di = DriveInfo.GetDrives();
            foreach (var info in di)
            {
                drives.Add(new DriveInformation() { Letter = info.Name.Remove(1), Label = info.IsReady ? info.VolumeLabel : string.Empty });
            }
        }

        internal class DriveInformation
        {
            public string Letter { get; set; }
            public string Label { get; set; }
            public string Path { get { return Letter + ":\\"; } }
            public override string ToString()
            {
                return "[ " + Letter + ": ]  -  " + Label;
            }
        }
    }
}
